const wsurl = 'ws://10.2.3.116:8080/server';
const DEBUG = true;

function print(elemId, msg) {
	if (DEBUG)
		$(elemId).innerHTML += msg + '<br /\>\n';
}


var testServerResponse = {body : 'WAZZUUUUUUP!', headers : {"content-length" : 12, "access-control-allow-origin" : '*'}, statusCode : 200, reasonPhrase : 'OK', httpVersion :"HTTP/1.1" };


var gateway = {
	execQueue : [],
	ws : null,
	initSocket : function() {
		this.ws = new WebSocket(wsurl);

		this.ws.state = {};

		this.ws.onopen = function() {
			print('progress', 'WebSocket opened!');
		};

		this.ws.onmessage = function(data) {
			print('received', data.data);
			gateway.parseMsg(data.data);
		};

		this.ws.onclose = function() {
			delete ws;
			ws = null;
			print('progress', 'WebSocket closed!');
		};

	},
	sendObject : function(data) {
		if (this.ws == null) {
			print('error', 'WebSocket is closed!');
			return;
		}
		var json = Object.toJSON(data);
		this.ws.send(json);
		print('sent', json);
	},
	register : function(pathRegex) {
		this.sendObject({ revhttp : { Method : 'REGISTER', pathRegex : pathRegex } });
	},
	request : function() {		
		if (gateway.ws.state != undefined && gateway.ws.state.listening)
			return;
		this.sendObject({ revhttp : { Method : 'REQUEST', pathRegex : this.ws.state.pathRegex,
			registrationKey : this.ws.state.registrationKey} });
	},
	requestResponse : function(reqId) {
		this.sendObject({ revhttp : {Method : "REQUEST-RESPONSE", pathRegex : this.ws.state.pathRegex,
			registrationKey : this.ws.state.registrationKey, requestId : reqId },
			http : testServerResponse});
	},
	unrequest : function() {
		this.sendObject({ revhttp : {Method : "UNREQUEST", pathRegex : this.ws.state.pathRegex,
			registrationKey : this.ws.state.registrationKey }});
	},
	unregister : function(pathRegex) {
		if (this.ws.state.registrationKey != undefined)
			this.sendObject({ revhttp : { Method : 'UNREGISTER', pathRegex : pathRegex,
				registrationKey : this.ws.state.registrationKey } });
	},
	parseMsg : function(data) {
		var json = data.evalJSON();
		var handler = methodHandlers[json.revhttp.Method];
		if (handler)
			handler(json);
		else
			print('error', 'No handler for method ' + json.revhttp.Method + ' defined.');
	}
};

var methodHandlers = {
	'REGISTER-RESPONSE' : function(json) {
		var revhttp = json.revhttp;
		if (revhttp.registrationKey != undefined) {
				gateway.ws.state = revhttp;		
			if (gateway.onRegister != undefined)
				gateway.onRegister();
		}
		return revhttp.success === "true";
	},
	'REQUEST-RESPONSE' : function(json) {
		gateway.ws.state.listening = json.revhttp.success === "true";
		if (gateway.onListening != undefined)
			gateway.onListening();
		return gateway.ws.state.listening;

	},
	'REQUEST' : function(json) {
		if (json.revhttp.requestId != undefined) {
			gateway.requestResponse(json.revhttp.requestId);
			return true;
		} else
			return false;
	},
	'UNREQUEST-RESPONSE' : function(json) {
		gateway.ws.state.listening = false;		
		if (gateway.onStopListening != undefined)
			gateway.onStopListening();
		return json.revhttp.success === "true";
	},
	'UNREGISTER-RESPONSE' : function(json) {
		gateway.ws.state = {};
		if (gateway.onUnregister != undefined)
			gateway.onUnregister();
		return json.revhttp.success === "true";
	}
};

